package cn1shared.util;


import com.codename1.ui.Image;

public class ImageUtil {

	private ImageUtil() {} // never

	public static Image limitSize(Image img, int size) {
		if (img.getWidth() > size)
			return img.scaled(size, -1);
		else if (img.getHeight() > size)
			return img.scaled(-1, size);
		return img;
	}

	public static Image toSquare(Image img) {
		int w = img.getWidth();
		int h = img.getHeight();
		if (w == h)
			return img;
		else if (w > h) {
			int diff2 = (w - h)/2;
			return img.subImage(diff2, 0, h, h, true);
		} else {
			int diff2 = (h - w)/2;
			return img.subImage(0, diff2, w, w, true);
		}
	}
}
